import characterTools from "server/component/model/characterTools";
import Log4R from "shared/component/log/Log4R";
import { Move2DServiceImpl } from "shared/modules/act/service/Move2DServiceImpl";

const loginfo = Log4R.getLog('info');
export default class Move2DServerServiceImpl extends Move2DServiceImpl {

    public addForces(characterEntity: Ctn.character.CharacterEntity): void {
        const character = characterEntity.character
        if (character) {
            this.addPositionController(character).Enabled = false
            this.addCatchController(character);
            this.addLinearVelocity(character);
            this.addAlignOrientation(character)
        }

        this.changeMass(characterEntity)
    }
    public changeMass(characterEntity: Ctn.character.CharacterEntity): void {
        /** 角色模型 */
        const character = characterEntity.character
        if (character) {
            /** 整体质量 */
            const defaultTotalmass = this.defaultMass?.Value;
            if (defaultTotalmass)
                characterTools.changeTotalMass(character, defaultTotalmass)
        }
    }
    /**
     * 添加trail抓取用的控制器
     * @param starterCharacter 
     */
    private addCatchController(starterCharacter: Model) {
        const handle = starterCharacter.FindFirstChild('Trail');
        let force = new Instance('AlignPosition');
        force.Parent = handle;
        force.Name = "CatchController";
        force.Enabled = false;

    }
    private addLinearVelocity(character: Ctn.character.HumanModel) {

        const actVelocity = new Instance('LinearVelocity', character.HumanoidRootPart);
        actVelocity.Name = 'ActVelocity';
        actVelocity.Enabled = false;
        actVelocity.Attachment0 = character.HumanoidRootPart.WaitForChild(characterTools.getRootAttName()) as Attachment;
        actVelocity.RelativeTo = Enum.ActuatorRelativeTo.Attachment0;
    }
    private addAlignOrientation(character: Ctn.character.HumanModel) {
        const alignOrientation = new Instance('AlignOrientation', character.HumanoidRootPart)
        alignOrientation.Attachment0 = character.HumanoidRootPart.FindFirstChild(characterTools.getRootAttName()) as Attachment;
        alignOrientation.RigidityEnabled = true;
        alignOrientation.Name = 'AlignOrientation';
        alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
    }
    private addPositionController(starterCharacter: Model) {
        const alignPosition = new Instance('AlignPosition');// 添加位置
        alignPosition.Parent = starterCharacter.PrimaryPart;
        alignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment;
        alignPosition.Enabled = false;
        const Attachment0 = starterCharacter.PrimaryPart?.FindFirstChild(characterTools.getRootAttName());
        if (Attachment0?.IsA('Attachment')) {
            alignPosition.Attachment0 = Attachment0
        }
        alignPosition.RigidityEnabled = true;
        alignPosition.Name = "AlignPosition";
        return alignPosition
    }
}